package cn.edu.seu.cose.qol.ws.resource.questionnaire;

import org.restlet.ext.xml.DomRepresentation;
import org.restlet.representation.Representation;
import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Put;
import cn.edu.seu.cose.qol.model.dto.question.QuestionInstance;
import cn.edu.seu.cose.qol.ws.resource.QolServerResource;
import cn.edu.seu.cose.qol.ws.resource.util.DomRepresentationBuilder;
import cn.edu.seu.cose.qol.ws.resource.util.XmlObjectReader;

public class QuestionResource extends QolServerResource {

	/**
	 * @return URI所指代的题目表征
	 */
	@Get("xml")
	public Representation getQuestion() {
		try {
			QuestionInstance question = new QuestionInstance(); // TODO 获取题目
			
			DomRepresentationBuilder builder =
					DomRepresentationBuilder.getInstance();
			return builder.getRepresentation(question);
		} catch (Exception e) {
			return getRepresentationByException(e);
		}
	}
	
	/**
	 * @param representation
	 * @return 更新URI所指代的题目。若题目已存在，则更新；若不存在，则创建。
	 */
	@Put("xml:xml")
	public Representation updateQuestion(
			Representation representation) {
		try {
			DomRepresentation inputRepresentation =
					(DomRepresentation) representation;
			XmlObjectReader reader = XmlObjectReader.getInstance();
			
			QuestionInstance updateQuestion = (QuestionInstance) reader.getXmlObject(
					inputRepresentation, QuestionInstance.class);
			
			// TODO 执行更新操作，返回更新、新建信息
			return null;
		} catch (Exception e) {
			return getRepresentationByException(e);
		}
	}

	/**
	 * 删除URI所指代的题目。
	 * @param representation
	 * @return 删除结果的表征
	 */
	@Delete("xml:xml")
	public Representation removeQuestion(
			Representation representation) {
		return null;
	}

}
